package com.zzyl.nursing.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.vo.TimeCountVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约信息Service业务层处理
 * 
 * @author alexis
 * @date 2025-08-18
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }



    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }


    /**
     * 新增预约信息
     * @param reservationDto
     * @return
     */
    @Override
    public int insertReservationmsg(ReservationDto reservationDto) {
        Long userId = UserThreadLocal.getUserId();
        Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        reservation.setCreateBy(String.valueOf(userId));
        reservation.setUpdateBy(String.valueOf(userId));
        reservation.setStatus(0);

        try {
          return save( reservation) ? 1:0;
        } catch (Exception e) {
            throw new BaseException("不能重复预约哦");

    }
    }




    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @Override
    public List<TimeCountVo> countReservationsForEachTimeWithinTimeRange(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        // 今天开始时间
        LocalDateTime startTime = localDateTime.toLocalDate().atStartOfDay();
        // 今天结束时间
        LocalDateTime endTime = startTime.plusDays(1);
        List<TimeCountVo> timeCountVoList=reservationMapper.countReservationsforTime(startTime, endTime);

        return timeCountVoList;
    }

    /**
     * 查询当天取消预约数量
     * @param userId
     * @return
     */
    @Override
    public int getCancelledReservationCount(Long userId) {
        // 今天开始时间
        LocalDateTime startTime = LocalDate.now().atStartOfDay();
        // 今天结束时间
        LocalDateTime endTime = startTime.plusDays(1);
       // 查询条件
        LambdaQueryWrapper<Reservation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Reservation::getUpdateBy, userId)
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getUpdateTime, startTime, endTime);
        // 统计数量
        return (int) count(lambdaQueryWrapper);
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }
}
